package example.objectecho;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;

import io.netty.buffer.ByteBuf;
import io.netty.buffer.Unpooled;
import io.netty.channel.ChannelFuture;
import io.netty.channel.ChannelFutureListener;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.SimpleChannelInboundHandler;
import io.netty.util.CharsetUtil;

public class ObjectEchoClientHander extends SimpleChannelInboundHandler<Object> {

	List<Integer> listMsg  ;
 

	public ObjectEchoClientHander() {
		listMsg = new ArrayList<Integer>();
		for (int i = 0; i < 100; i++) {
			listMsg.add(i);
		}
	}

	
	@Override
	public void channelActive(ChannelHandlerContext ctx) throws Exception {
		// TODO Auto-generated method stub
		System.out.println("active");
		// Initialize the message.
		
		// send the message.
		ctx.writeAndFlush(listMsg);
		
	}
	
	@Override
	protected void channelRead0(ChannelHandlerContext ctx, Object msg) throws Exception {
		System.out.println("read0");
		
		if(msg instanceof List){
			 System.out.println( "is list");
			 List listMsg = ((List) msg);
			 System.out.println("size：" + listMsg.size() + " last element:" + listMsg.get(listMsg.size()-1));
			
		}
		 
	}


	@Override
	public void channelInactive(ChannelHandlerContext ctx) throws Exception {
		
		System.out.println("conn inactive");
	}

	@Override
	public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) throws Exception {
		// Close the connection when an exception is raised.
		cause.printStackTrace();
		ctx.close();
	}

}
